import axios from "axios"

export interface globalSearchObj {
    value: string,
    address: string,
}

export class GlobalSearcher {

    globalSearchObjsList: Array<globalSearchObj>

    constructor(globalSearchObjectPath: string) {
        this.globalSearchObjsList = new Array()
        // fs.readFile(globalSearchObjectPath, (err, data) => {
        //     if (err)
        //         console.error(err)
        //     else {
        // var origin_data = data.toString().split('\n')
        // for (var idx = 1; idx < origin_data.length; idx += 1) {
        //     var row_data = origin_data[idx]
        //     if (row_data[row_data.length - 1] == '\r')
        //         row_data = row_data.substring(0, row_data.length - 1)
        //     var obj_data = row_data.split(',')
        //     this.globalSearchObjsList.push({
        //         value: obj_data[0],
        //         address: obj_data[1]
        //     })
        //         }
        //     }
        // })
        axios({
            url: '/api/static/configs/GlobalSearchObject.lunaria',
        }).then((res) => {
            if (res) {
                // console.log(res.data)
                var origin_data = res.data.toString().split('\n')
                for (var idx = 1; idx < origin_data.length; idx += 1) {
                    var row_data = origin_data[idx]
                    if (row_data[row_data.length - 1] == '\r')
                        row_data = row_data.substring(0, row_data.length - 1)
                    var obj_data = row_data.split(',')
                    this.globalSearchObjsList.push({
                        value: obj_data[0],
                        address: obj_data[1]
                    })
                }
            }
        })
    }

    getGlobalSearchObjsList(): Array<globalSearchObj> {
        return this.globalSearchObjsList
    }
}

function getGlobalSearcher(path: string) {
    return new GlobalSearcher(path)
}

export default {
    getGlobalSearcher
}